/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller.Pronosticos;

import Entity.Periodo;
import java.util.ArrayList;

/**
 *
 * @author camilourd
 */
public class WinterMultiplicativo extends MetodosPronosticos {

    public WinterMultiplicativo() {
        super();
    }
    
    public void addPeriodo(String name, double demand, double ft, double tt, double it) {
        dmp.getPers().add(new Periodo(name, demand, ft, tt, it));
    }
    
    public void calcular(int numPers) {
        ArrayList<Periodo> pers = dmp.getPers();
        double ytsum = 0;
        int n = pers.size();
        
        if(n > 0) {
            for(Periodo p: pers) ytsum += p.getDemanda();
            
            pers.get(0).setFt(ytsum / (1.0 * n));
            pers.get(0).setIt(1.0);

            double alpha = dmp.getAlpha();
            double beta = dmp.getBeta();
            double gamma = dmp.getGamma();
            double invalpha = 1.0 - alpha;
            double invbeta = 1.0 - beta;
            double invgamma = 1.0 - gamma;
            double data;

            for(int i = 1;i < n; ++i) {
                // calcular Ft
                data = ((alpha * pers.get(i).getDemanda()) / pers.get(i - 1).getIt())
                        + (invalpha * (pers.get(i - 1).getFt() + pers.get(i - 1).getTt()));
                pers.get(i).setFt(data);
                // calcular Tt
                data = (beta * (pers.get(i).getFt() - pers.get(i - 1).getFt()))
                        + (invbeta * pers.get(i - 1).getTt());
                pers.get(i).setTt(data);
                // calcular It
                data = (gamma * (pers.get(i).getDemanda() / pers.get(i).getFt()))
                        + (invgamma * pers.get(i - 1).getIt());
                pers.get(i).setIt(data);
            }

            // calcular pronósticos
            double ftn = pers.get(n - 1).getFt();
            double ttn = pers.get(n - 1).getTt();
            double itn = pers.get(n - 1).getIt();
            dmp.getYp().add(pers.get(n - 1).getDemanda());

            for(int i = 1;i <= numPers; ++i) {
                data = (ftn + (ttn * i)) * itn;
                if(data < 0.0) data = 0.0;
                dmp.getYp().add(data);
            }

            double ypn = dmp.getYp().get(1);
            double ytn = pers.get(n - 1).getDemanda();
            // calcular errores
            calcularErrores(ytn, ypn, n * 1.0);
        }
    }
    
    public void obtenerFTI(javax.swing.JTable table) {
        ArrayList<Periodo> pers = dmp.getPers();
        int n = pers.size();
        
        for(int i = 0;i < n; ++i) {
            table.setValueAt(aproxDouble(pers.get(i).getFt()), i, 2);
            table.setValueAt(aproxDouble(pers.get(i).getTt()), i, 3);
            table.setValueAt(aproxDouble(pers.get(i).getIt()), i, 4);
        }
    }
}
